package com.arcbbs.controller;

import com.arcbbs.annotation.GlobalInterceptor;
import com.arcbbs.entity.*;
import com.arcbbs.enums.MessageTypeEnum;
import com.arcbbs.exception.BaseException;
import com.arcbbs.mapper.*;
import com.arcbbs.result.PageResult;
import com.arcbbs.result.Result;
import com.arcbbs.service.FormArticleService;
import com.arcbbs.service.UserInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private FormArticleMapper formArticleMapper;
    @Resource
    private LikeRecordMapper likeRecordMapper;
    @Resource
    private FormArticleService formArticleService;
    @Resource
    private UserMessageMapper userMessageMapper;

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserIntegralRecordMapper userIntegralRecordMapper;

    @RequestMapping("/info")
    public Result info(String userId) {
        UserInfo userInfo = userInfoMapper.getinfobyuserid(userId);
        if (null == userInfo || userInfo.getStatus().equals(0)) {
            throw new BaseException("账号被禁用或不存在");

        }
        Integer postcount = formArticleMapper.selectbycount(userId, 1);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        userInfoVo.setPostCount(postcount);

        Integer likecount = likeRecordMapper.selectcountbyuserId(userId);
        userInfoVo.setLikeCount(likecount);

        return Result.success(userInfoVo);
    }

    @RequestMapping("/loadarticle")
    public Result loadarticle(HttpSession httpSession, String userId, Integer type) {
        UserInfo userInfo = userInfoMapper.getinfobyuserid(userId);
        ForumArticleQuery forumArticleQuery = new ForumArticleQuery();
        forumArticleQuery.setOrder("post_time desc");
        if (null == userInfo || userInfo.getStatus().equals(0)) {
            throw new BaseException("账号被禁用或不存在");

        }
        if (type == 0) {
            forumArticleQuery.setUserId(userId);
        } else if (type == 1) {
            forumArticleQuery.setCommentUserId(userId);
        } else if (type == 2) {
            forumArticleQuery.setLikeUserId(userId);

        }
//        SessionWebUserDto userDto=(SessionWebUserDto) httpSession.getAttribute("sessionKey");
//        if(userDto !=null){
//forumArticleQuery.setCurrentUserId(userDto.getUserId());
//        }else{
//forumArticleQuery.setStatus(1);
//        }
        forumArticleQuery.setPageNo(1);
        System.out.println(forumArticleQuery.getUserId());
        System.out.println(forumArticleQuery.getCurrentUserId());
        System.out.println(forumArticleQuery.getLikeUserId());
        System.out.println(forumArticleQuery.getCommentUserId());
        PageHelper.startPage(forumArticleQuery.getPageNo(), 10);
        PageResult result = new PageResult();
        Page<ForumArticle> page = formArticleMapper.page1(forumArticleQuery);
        long total = page.getTotal();


        List<ForumArticle> list = page.getResult();
        result.setRecords(list);
        result.setTotal(total);
        return Result.success(result);
    }

    @RequestMapping("/updateuserinfo")
    public Result updateuserinfo(HttpSession httpSession, Integer sex, String personDescription, MultipartFile avatar) {
        SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) httpSession.getAttribute("sessionKey");
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(sessionWebUserDto.getUserId());
        userInfo.setSex(sex);
        userInfo.setPersonDescription(personDescription);
        userInfoService.update(userInfo, avatar);
        return Result.success();

    }

    @RequestMapping("/loadUserIntegralRecord")
    public Result loadUserIntegralRecord(HttpSession httpSession, Integer pageNo, Integer pageSize) {
        SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) httpSession.getAttribute("sessionKey");
        UserIntegralRecord userIntegralRecord = new UserIntegralRecord();
        PageHelper.startPage(pageNo, 10);
        PageResult result = new PageResult();
        Page<UserIntegralRecord> page = userIntegralRecordMapper.page(sessionWebUserDto.getUserId());
        long total = page.getTotal();
        result.setRecords(page);
        result.setTotal(total);
        return Result.success(result);

    }

    @RequestMapping("/message")
    public Result message(String userId) {
        List<Map> list = userInfoMapper.getmessage(userId);
        System.out.println(list);
        UserMessageCountDto userMessageCountDt = new UserMessageCountDto();
        Long totalCount = 0L;
        for (Map item : list) {

            Long count = (Long) item.get("count");
            totalCount = totalCount + count;
            Integer type = (Integer) item.get("messageType");
            switch (type) {
                case 0:
                    userMessageCountDt.setSys(count);
                    break;
                case 1:
                    userMessageCountDt.setReply(count);
                    break;
                case 2:
                    userMessageCountDt.setLikePost(count);
                    break;
                case 3:
                    userMessageCountDt.setLikeComment(count);
                    break;
                case 4:
                    userMessageCountDt.setDownloadAttachment(count);
                    break;


            }
            userMessageCountDt.setTotal(totalCount);
        }
        return Result.success(userMessageCountDt);
    }
    @RequestMapping("/loadmessage")
    public Result messagelist(HttpSession httpSession,String code,Integer pageNo) {
        MessageTypeEnum messageTypeEnum=MessageTypeEnum.getByCode(code);
        if(messageTypeEnum == null){
            throw new BaseException("类型不存在");
        }
        SessionWebUserDto sessionWebUserDto=(SessionWebUserDto) httpSession.getAttribute("sessionKey");
        PageHelper.startPage(pageNo, 10);
        PageResult result = new PageResult();
        System.out.println(messageTypeEnum.getType());
        System.out.println(sessionWebUserDto.getUserId());
        Page<UserMessage> page = userMessageMapper.page(sessionWebUserDto.getUserId(),messageTypeEnum.getType());
        long total = page.getTotal();
        System.out.println(page);
        result.setRecords(page);
        result.setTotal(total);
        userMessageMapper.updateByUserId(sessionWebUserDto.getUserId(),messageTypeEnum.getType());


        return Result.success(result);

    }
}

